package com.qc.bus.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.bean.copier.CopyOptions;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qc.bus.dao.BusCustomerDao;
import com.qc.bus.entity.BusCustomer;
import com.qc.bus.entity.BusGoods;
import com.qc.bus.service.BusCustomerService;
import com.qc.bus.vo.CustomerVo;
import com.qc.sys.common.Constant;
import com.qc.sys.common.DataGridView;
import com.qc.sys.common.ResultObj;
import io.swagger.models.auth.In;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import java.util.function.Consumer;

/**
 * (BusCustomer)表服务实现类
 *
 * @author AltmanQccc
 * @since 2020-09-08 20:01:32
 */
@Service
@Transactional
@EnableCaching
public class BusCustomerServiceImpl extends ServiceImpl<BusCustomerDao, BusCustomer> implements BusCustomerService {

    @Autowired
    private BusCustomerDao customerDao;

    @Override
    public DataGridView queryAllCustomer(CustomerVo customerVo) {
        IPage<BusCustomer> page = new Page(customerVo.getPage(),customerVo.getLimit());
        QueryWrapper<BusCustomer> qw = new QueryWrapper<>();
        qw.eq(customerVo.getAvailable()!=null,"available",customerVo.getAvailable());
        qw.like(StringUtils.isNotBlank(customerVo.getCustomername()),"customername",customerVo.getCustomername());
        qw.like(StringUtils.isNotBlank(customerVo.getConnectionperson()),"connectionperson",customerVo.getConnectionperson());
        if(StringUtils.isNotBlank(customerVo.getPhone())) {
            qw.and(new Consumer<QueryWrapper<BusCustomer>>() {
                @Override
                public void accept(QueryWrapper<BusCustomer> busCustomerQueryWrapper) {
                    qw.like(StringUtils.isNotBlank(customerVo.getPhone()), "phone", customerVo.getPhone())
                            .or().like(StringUtils.isNotBlank(customerVo.getPhone()), "telephone", customerVo.getPhone());
                }
            });
        }
        this.customerDao.selectPage(page,qw);
        return new DataGridView(page.getTotal(),page.getRecords());
    }

    @CachePut(cacheNames = "BusCustomerServiceImpl",key = "#result.id")
    @Override
    public BusCustomer InsertCustomer(BusCustomer customer) {
        this.customerDao.insert(customer);
        return customer;
    }

    @CachePut(cacheNames = "BusCustomerServiceImpl",key = "#result.id")
    @Override
    public BusCustomer UpdateCustomer(BusCustomer customer) {
        BusCustomer busCustomer = this.customerDao.selectById(customer.getId());
        BeanUtil.copyProperties(customer,busCustomer, CopyOptions.create().setIgnoreNullValue(true).setIgnoreError(true));
        this.customerDao.updateById(busCustomer);
        return busCustomer;
    }

    @CacheEvict(cacheNames = "BusCustomerServiceImpl",key = "#id")
    @Override
    public boolean removeById(Serializable id) {
        return super.removeById(id);
    }

    @Cacheable(cacheNames = "BusCustomerServiceImpl",key = "#id" )
    @Override
    public BusCustomer getById(Serializable id) {
        return super.getById(id);
    }

    @Override
    public ResultObj batchDeleteCustomer(Integer[] ids) {
        //1.对ids进行判断，是否为空，是否长度大于0
        //2.对ids进行遍历删除
        //3.false执行 {code:-1,msg:"传入ids异常"}
        try {
            //1.对ids进行判断，是否为空，是否长度大于0
            if (null!=ids&&ids.length>0){
                //2.对ids进行遍历删除
                List<Integer> idList = new ArrayList<>();
                for (Integer id : ids) {
                    idList.add(id);
                }
                this.customerDao.deleteBatchIds(idList);
                return ResultObj.DELETE_SUCCESS;
            } else {
                //3.false执行 {code:-1,msg:"传入ids异常"}
                return new ResultObj(-1,"传入ids异常");
            }
        } catch (Exception e) {
            e.printStackTrace();
            return ResultObj.DELETE_ERROR;
        }
    }

    @Override
    public DataGridView queryAllAvailableCustomer() {
        QueryWrapper<BusCustomer> qw = new QueryWrapper<>();
        qw.eq("available", Constant.USER_AVAILABLE_TRUE);
        List<BusCustomer> customers = this.customerDao.selectList(qw);
        return new DataGridView(customers);
    }
}